#import "AddressListViewController.h"
#import "JsonUtil.h"
#import "PopUIManager.h"
#import "NetWorkManager.h"

@interface AddressListViewController ()

@end

@implementation AddressListViewController
@synthesize myTableView, addr_list, addr_string;

- (void)viewDidLoad {
    [super viewDidLoad];
    myTableView.dataSource = self;
    myTableView.delegate = self;
    //去除多余的分割线
    myTableView.tableFooterView = [[UIView alloc]init];
    addr_string = @"";
    str_schoolid = @"";
}

- (IBAction)backButtonClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return addr_list.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_tag"];
    if (cell == nil) {
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell_tag"];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"%@",[[addr_list objectAtIndex:indexPath.row] objectForKey:@"name"]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *tempString = [NSString stringWithFormat:@"%@", [[addr_list objectAtIndex:indexPath.row]objectForKey:@"name"]];
    addr_string = [addr_string stringByAppendingString:tempString];
    
    NSString *add_id = [[addr_list objectAtIndex:indexPath.row]objectForKey:@"id"];
    [self requestServerForAddress:add_id];
    if (str_schoolid.length == 0) {
        str_schoolid = add_id;
    }else{
        str_schoolid = [NSString stringWithFormat:@"%@_%@",str_schoolid, add_id];
    }
    NSLog(@"school connnect string >> %@",str_schoolid);
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selected = NO;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}

-(void)requestServerForAddress:(NSString *) p1{
    NSString *dataString = [JsonUtil convertDictionaryToJson:[[NSDictionary alloc]initWithObjectsAndKeys:@"0",@"type",p1,@"id", nil]];
    
    NSString *msg = @"GET_ZONE_LIST";
    
    PopUIManager *popUIMgr = [PopUIManager getInstanse];
    [popUIMgr showWaitingUI:self.view];
    
    [[NetWorkManager getInstanse] requestURLAsynMessage:msg data:dataString FinishBlock:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (connectionError) { //连接失败
            NSLog(@"Http Error: %@   %ld", connectionError.localizedDescription,(long)connectionError.code);
            NSString *error_content = [NSString stringWithFormat:@"连接错误:%ld",(long)connectionError.code];
            [popUIMgr popAlertView:nil title:@"提示" message:error_content cancelButtonTitle:@"确定"];
        }else{//连接成功，判断是否有数据返回
            NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
            NSLog(@"服务器返回StateCode:%ld", (long)responseCode);
            NSDictionary *dict = [JsonUtil convertDataToDictionary:data];
            NSLog(@"%@",dict);
            if (data) {
                NSDictionary *dict = [JsonUtil convertDataToDictionary:data];
                if ([[dict objectForKey:@"retCode"] integerValue]== 1) {
                    //获取到地址数据
                    NSArray *temp_list = [dict objectForKey:@"data"];
                    //重新reload tableView
                    dispatch_async(dispatch_get_main_queue(), ^{
//NSLog(@"%@",temp_list);
                        addr_list = temp_list;
                        [myTableView reloadData];
                    });
                }else{
                    NSLog(@"Address: %@",addr_string);
                    [self.myDelegate passAddress:addr_string schoolId:str_schoolid];
                    [popUIMgr removeWaitingUI];
                    [self dismissViewControllerAnimated:YES completion:nil];
                }
            }else{
                [popUIMgr popAlertView:nil title:@"提示" message:@"服务器没有返回数据" cancelButtonTitle:@"确定"];
            }
        }
        [popUIMgr removeWaitingUI];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
